Grokking SQL for Tech Interviews
Rate this course
Ask Author
Contribute
Back to course home

1% completed

Vote For New Content
Introduction
Course Overview
What is RDBMS?
What is SQL?
A Quick Review of SQL
Read data from table
WHERE Clause
Operators
Handle NULLs in SQL
DISTINCT Clause
ORDER BY Clause
LIMIT and OFFSET
GROUP BY
HAVING
Aggregate Functions
Order of Execution
Order of SQL Query Execution
SQL Functions
Number Functions
String Functions
Date Functions
Nested Queries
Nested Query
Types of Subqueries
JOINS
Introduction
INNER JOIN
LEFT OUTER JOIN
RIGHT OUTER JOIN
FULL OUTER JOIN
SELF JOIN
SET Operations
What are SET operations?
UNION
INTERSECT
EXCEPT
A Quick Review of DDL
Keys in SQL
CREATE
ALTER
DROP
TRUNCATE
Indexes
A Quick Review of DML
INSERT
UPDATE
DELETE
Database Backup & Restore
Backup Database
Restore Database
Interview Problems - Easy
1. Major Cities
2. Not boring movies
3. Triangle Judgement
4. Biggest Single Number
5. Product Sales Analysis I
6. Product Sales Analysis II
7. Game Play Analysis I
8. Game Play Analysis II
9. Queries Quality and Percentage
10. Average Selling Price
11. Weather Type in Each Country
12. Students and Examinations
13. Ads Performance
14. Group Sold Products By The Date
15. Employee Email Verification
16. Sellers With No Sales
17. Average Time of Process
18. Find Followers Count
19. Ad-Free Sessions
20. Bikes Last Time Used
21. Daily User Engagement Levels
22. Employee Attendance Record
23. Library Book Loans
24. Customer Purchase Summary
25. Long Comments (Easy)
26. Unused Accounts
27. Patient Appointment No-Shows
28. Books and Authors
29. High School Attendance
30. Employee Absences
31. Green Product Identification (Easy)
32. Employee Department Exclusion (Easy)
33. Sales Person (Easy)
34. Employee Task Tracking (Easy)
Interview Problems - Medium
1. Winning Candidate
2. Second Degree Follower
3. 2nd Highest Salary
4. Game Play Analysis III
5. Game Play Analysis IV
6. Active Businesses
7. Product Price on a Specific Date
8. Monthly Transactions II
9. Team Scores in Football Tournament
10. Page Recommendations
11. Running Total for Different Genders
12. Evaluate Boolean Expression
13. Active Users
14. Find Interview Candidates
15. Suspicious Bank Accounts
16. Order Fulfillment Rate (Medium)
17. Last Person to Fit in the Elevator (Medium)
18. Retail Expansion Analysis (Medium)
19. Employee Collaboration Networks (Medium)
20. Second Fastest Time in Races (Medium)
1. Trips and Users
2. Human Traffic of Stadium
3. Students Report By Geography
4. Game Play Analysis V
5. Number of Transactions per Visit
6. Find the Quiet Students in All Exams
7. First and Last Call On the Same Day
8. Build the Equation
9. Longest Winning Streak
10. Dynamic Pivoting of a Table
11. Dynamic Unpivoting of a Table
12. Generate the Invoice
13. Popularity Percentage
14. Viewers Turned Streamers
15. Status of Flight Tickets
1. High Performing Employees (Easy)
2. Student Course Averages (Easy)
3. Library Book Checkout Percentage (Easy)
4. Employee Performance and Improvement Rate (Easy)
5. Find Popular Posts (Easy)
6. Main Office Assignment for Each Employee (Easy)
7. Rectangle Validation (Easy)
8. Library Book Checkouts by Date (Easy)
9. Same-Day Appointment Fulfillment (Medium)
10. Departments with High Earning Employees (Medium)
11. Students Who Attended All Courses (Medium)
12. Library Book Checkouts First Year (Medium)
13. Repeated Values in Sequence (Medium)
14. School Top Achievers by Subject (Hard)
8. Monthly Transactions II

Problem

Table: Transactions

+----------------+---------+
| Column Name    | Type    |
+----------------+---------+
| id             | int     |
| country        | varchar |
| state          | enum    |
| amount         | int     |
| trans_date     | date    |
+----------------+---------+
id is the column of unique values of this table.
The table has information about incoming transactions.
The state column is an ENUM (category) of type ["approved", "declined"].

Table: Chargebacks

+----------------+---------+
| Column Name    | Type    |
+----------------+---------+
| trans_id       | int     |
| trans_date     | date    |
+----------------+---------+
Chargebacks contains basic information regarding incoming chargebacks from some transactions placed in Transactions table.
trans_id is a foreign key (reference column) to the id column of Transactions table.
Each chargeback corresponds to a transaction made previously even if they were not approved.

Problem Definition

Write a solution to the number of approved transactions and their total amount, the number of chargebacks, and their total amount find for each month and country. Return the records in the order of date.

Note: In your solution, given the month and country, ignore rows with all zeros.

Example

Image

Output

Image

Try It Yourself

MYSQL

. . . .

Transactions =

Chargebacks =

Solution

To determine the number of approved transactions and their total amount, the number of chargebacks and their total amount for each month and country, we can follow a structured approach that leverages SQL's aggregation and conditional functions. This ensures accurate and efficient computation by combining relevant data from the Transactions and Chargebacks tables.

  • Combine Chargebacks with Approved Transactions: Merge chargeback records with approved transactions, labeling chargebacks distinctly to differentiate them from regular approved transactions.
  • Aggregate Data by Month and Country: Calculate the count and total amount of approved transactions and chargebacks for each combination of month and country.
  • Filter Out Rows with All Zeros: Exclude any month-country combinations where both approved transactions and chargebacks are zero, focusing only on meaningful data.
  • Order the Results: Sort the final output chronologically by month to present the data in an organized manner.

SQL Query

SELECT month, country, Sum(CASE WHEN state = "approved" THEN 1 ELSE 0 END) AS approved_count, Sum(CASE WHEN state = "approved" THEN amount ELSE 0 END) AS approved_amount, Sum(CASE WHEN state = "back" THEN 1 ELSE 0 END) AS chargeback_count, Sum(CASE WHEN state = "back" THEN amount ELSE 0 END) AS chargeback_amount FROM (SELECT LEFT(Chargebacks.trans_date, 7) AS month, country, "back" AS state, amount FROM Chargebacks JOIN Transactions ON Chargebacks.trans_id = Transactions.id UNION ALL SELECT LEFT(trans_date, 7) AS month, country, state, amount FROM Transactions WHERE state = "approved") s GROUP BY month, country HAVING approved_count > 0 OR chargeback_count > 0 ORDER BY month;

Step-by-Step Approach

Step 1: Combine Chargebacks with Approved Transactions

Merge the Chargebacks and Transactions tables to create a unified dataset that distinguishes chargebacks from approved transactions. This involves labeling chargebacks with a unique state identifier and ensuring that only approved transactions are included from the Transactions table.

SQL Query:

SELECT LEFT(Chargebacks.trans_date, 7) AS month, country, "back" AS state, amount FROM Chargebacks JOIN Transactions ON Chargebacks.trans_id = Transactions.id UNION ALL SELECT LEFT(trans_date, 7) AS month, country, state, amount FROM Transactions WHERE state = "approved";

Explanation:

  • First Part (Chargebacks):

    • LEFT(Chargebacks.trans_date, 7) AS month:
      • Extracts the year and month from the trans_date field, formatting it as 'YYYY-MM'.
    • country:
      • Retrieves the country associated with the transaction.
    • "back" AS state:
      • Labels the record as a chargeback by assigning the state 'back'.
    • amount:
      • Captures the transaction amount.
    • JOIN Transactions ON Chargebacks.trans_id = Transactions.id:
      • Links each chargeback to its corresponding transaction to access the country and amount.
  • Second Part (Approved Transactions):

    • LEFT(trans_date, 7) AS month:
      • Extracts the year and month from the trans_date field, formatting it as 'YYYY-MM'.
    • country:
      • Retrieves the country associated with the transaction.
    • state:
      • Captures the state of the transaction, which is 'approved' based on the WHERE clause.
    • amount:
      • Captures the transaction amount.
    • WHERE state = "approved":
      • Filters the records to include only those transactions that were approved.
  • UNION ALL:

    • Combines the results from both parts into a single dataset without removing duplicates.

Output After Step 1:

Based on the provided input data, the combined dataset (s) will be:

monthcountrystateamount
2019-05USback2000
2019-06USback1000
2019-09USback5000
2019-05USapproved1000
2019-06USapproved3000
2019-06USapproved5000

Explanation of Output:

  • Chargebacks:
    • Transaction ID 102:
      • Chargeback date: 2019-05-29 → month: 2019-05
      • Amount: 2000
    • Transaction ID 101:
      • Chargeback date: 2019-06-30 → month: 2019-06
      • Amount: 1000
    • Transaction ID 105:
      • Chargeback date: 2019-09-18 → month: 2019-09
      • Amount: 5000
  • Approved Transactions:
    • Transaction ID 101:
      • Date: 2019-05-18 → month: 2019-05
      • Amount: 1000
    • Transaction ID 103:
      • Date: 2019-06-10 → month: 2019-06
      • Amount: 3000
    • Transaction ID 105:
      • Date: 2019-06-15 → month: 2019-06
      • Amount: 5000

Step 2: Aggregate Data by Month and Country

For each combination of month and country, calculate:

  • The number of approved transactions (approved_count).
  • The total amount of approved transactions (approved_amount).
  • The number of chargebacks (chargeback_count).
  • The total amount of chargebacks (chargeback_amount).

SQL Query:

SELECT month, country, Sum(CASE WHEN state = "approved" THEN 1 ELSE 0 END) AS approved_count, Sum(CASE WHEN state = "approved" THEN amount ELSE 0 END) AS approved_amount, Sum(CASE WHEN state = "back" THEN 1 ELSE 0 END) AS chargeback_count, Sum(CASE WHEN state = "back" THEN amount ELSE 0 END) AS chargeback_amount FROM ( -- Combined Data from Step 1 SELECT LEFT(Chargebacks.trans_date, 7) AS month, country, "back" AS state, amount FROM Chargebacks JOIN Transactions ON Chargebacks.trans_id = Transactions.id UNION ALL SELECT LEFT(trans_date, 7) AS month, country, state, amount FROM Transactions WHERE state = "approved" ) s GROUP BY month, country HAVING approved_count > 0 OR chargeback_count > 0 ORDER BY month;

Explanation:

  • SELECT month, country:
    • Groups the aggregated data by each month and country combination.
  • Sum(CASE WHEN state = "approved" THEN 1 ELSE 0 END) AS approved_count:
    • Counts the number of approved transactions by summing 1 for each record where the state is 'approved'.
  • Sum(CASE WHEN state = "approved" THEN amount ELSE 0 END) AS approved_amount:
    • Calculates the total amount of approved transactions by summing the amount where the state is 'approved'.
  • Sum(CASE WHEN state = "back" THEN 1 ELSE 0 END) AS chargeback_count:
    • Counts the number of chargebacks by summing 1 for each record where the state is 'back'.
  • Sum(CASE WHEN state = "back" THEN amount ELSE 0 END) AS chargeback_amount:
    • Calculates the total amount of chargebacks by summing the amount where the state is 'back'.
  • FROM (...) s:
    • Uses the combined dataset from Step 1 as a subquery aliased as s.
  • GROUP BY month, country:
    • Aggregates the sums and counts for each unique combination of month and country.
  • HAVING approved_count > 0 OR chargeback_count > 0:
    • Filters out any month-country combinations where both approved transactions and chargebacks are zero, as per the problem's requirement.
  • ORDER BY month:
    • Sorts the final results chronologically by month.

Output After Step 2:

Based on the combined dataset from Step 1, the aggregation would yield:

monthcountryapproved_countapproved_amountchargeback_countchargeback_amount
2019-06US2800011000
2019-05US1100012000
2019-09US0015000

Explanation of Output:

  • For May 2019 (US):
    • Approved Transactions:
      • 1 transaction (amount: 1000).
      • Total approved amount: 1000.
    • Chargebacks:
      • 1 chargeback (amount: 2000).
      • Total chargeback amount: 2000.
  • For June 2019 (US):
    • Approved Transactions:
      • 2 transactions (amounts: 3000, 5000).
      • Total approved amount: 8000.
    • Chargebacks:
      • 1 chargeback (amount: 1000).
      • Total chargeback amount: 1000.
  • For September 2019 (US):
    • Approved Transactions:
      • 0 transactions.
      • Total approved amount: 0.
    • Chargebacks:
      • 1 chargeback (amount: 5000).
      • Total chargeback amount: 5000.

Note:

  • The HAVING clause ensures that only records with at least one approved transaction or one chargeback are included. Thus, even though September 2019 has zero approved transactions, it is included because there is one chargeback.

Step 3: Order the Results by Month

Ensure that the final output is sorted in chronological order based on the month to provide a clear and organized view of the data over time.

SQL Query:

ORDER BY month;

Explanation:

  • ORDER BY month:
    • Sorts the aggregated results in ascending order of the month column, ensuring that earlier months appear before later ones.

Final Output:

Based on the aggregated data from Step 2, the final output after ordering would be:

monthcountryapproved_countapproved_amountchargeback_countchargeback_amount
2019-05US1100012000
2019-06US2800011000
2019-09US0015000

Explanation of Output:

  • The results are ordered chronologically by month (2019-05, 2019-06, 2019-09), providing a sequential view of transaction activities and chargebacks for the US.
7. Product Price on a Specific Date
9. Team Scores in Football Tournament
Mark as Completed

On this page

Problem

Problem Definition

Example

Output

Try It Yourself

Solution

SQL Query

Step-by-Step Approach